using System;
using System.Text;
using System.Web.UI.WebControls;
using eWorld.UI;
using KangaUtils;
using TimeSheet.Logger;

namespace TimeSheet.Protected.View.Model
{
    /// <summary>
    ///		Summary description for EditAdministrator.
    /// </summary>
    public abstract class EditAdministrator : EntityControl
    {
        protected Button btnSave;
        protected CheckBox Active;
        protected Label lblAddEditTitle;
        protected RequiredFieldValidator RequiredFieldValidator1;
        protected TextBox AdminName;
        protected RequiredFieldValidator RequiredFieldValidator2;
        protected RegularExpressionValidator RegularExpressionValidator1;
        protected TextBox Email;
        protected ValidationSummary ValidationSummary1;
        protected Label lblMessage;
        protected CollapsablePanel cpEntity;

        private void Page_Load(object sender, EventArgs e)
        {
            BindControlToEntity();
            if (BoundEntity.ID > 0)
            {
                lblAddEditTitle.Text = "Edit Administrator";
                btnSave.Text = "Update";
            }
            else
            {
                lblAddEditTitle.Text = "Add Administrator";
                btnSave.Text = "Create";
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            LoadEntityFromForm();
            bool sent = false;

            if (BoundEntity.ID <= 0)
            {
                try
                {
                    //we need to generate an administrator password
                    TimeSheetBLL.Administrator admin =
                        (TimeSheetBLL.Administrator) BoundEntity;
                    admin.Password =
                        SecurityUtils.GenerateRandomPassword(AppSettings.PasswordLength);
                    //send the password to the administrator's specified e-mail address
                    MailUtils.SendEmail(
                        AppSettings.SmtpServer,
                        GetAdministratorMailSubject(),
                        GetAdministratorMailBody(admin),
                        AppSettings.AdminEmail,
                        admin.Email);
                    sent = true;
                }
                catch (Exception ex)
                {
                    ExceptionLogger.AddException(ex);
                    lblMessage.Text =
                        "There was an error sending the notification e-mail to the new administrator.";
                }
            }

            SaveBoundEntity();

            if (sent)
            {
                Response.Redirect("~/Protected/Administrator.aspx");
            }
        }

        private string GetAdministratorMailBody(TimeSheetBLL.Administrator admin)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<p>You have been registered as a Timesheet Administrator. </p>");
            sb.Append(
                "<p>To log in, go to this location: http://" + AppSettings.AdminURL +
                " and enter in ");
            sb.Append("the following information: </p>");
            sb.Append("<p>User Name: " + admin.AdminName + "<br />");
            sb.Append("Password: " + admin.Password + "</p>");

            return sb.ToString();
        }

        private string GetAdministratorMailSubject()
        {
            return "Your Timesheet Password";
        }
    }
}